package com.hzw.code.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * 系统工具类
 */
@Slf4j
public class SystemUtils {
    /**
     * 获取本机物理地址
     * @return
     */
    public static String getMacAddress() {
        InetAddress inetAddress = getInetAddress();
        if(inetAddress == null){
            return null;
        }
        return getLocalMac(inetAddress);
    }

    /**
     * 获取本机ip
     * @return
     */
    public static String getLocalIp() {
        InetAddress inetAddress = getInetAddress();
        if(inetAddress == null){
            return null;
        }
        return inetAddress.getHostAddress();
    }

    private static InetAddress getInetAddress(){
        InetAddress inetAddress = null;
        String name;
        String displayName;
        try {
            OK:
            for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements(); ) {
                NetworkInterface networkInterface = interfaces.nextElement();

                name = networkInterface.getName();
                displayName = networkInterface.getDisplayName();
                if(name != null && (name.indexOf("docker") == 0 || name.indexOf("flannel") == 0)){
                    continue;
                }
                if(displayName != null && (displayName.indexOf("VirtualBox") == 0 || displayName.indexOf("VMware") == 0)){
                    continue;
                }
                if (networkInterface.isLoopback()
                        || networkInterface.isVirtual()
                        || !networkInterface.isUp()) {
                    continue;
                }
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    inetAddress = addresses.nextElement();
                    if (inetAddress instanceof Inet4Address) {
                        break OK;
                    }
                }
            }
        } catch (SocketException e) {
            log.error(e.toString());
        }
        return inetAddress;
    }

    private static String getLocalMac(InetAddress ia)  {
        //获取网卡，获取地址
        try {
            NetworkInterface networkInterface = NetworkInterface.getByInetAddress(ia);
            if(networkInterface == null){
                return null;
            }
            byte[] mac = networkInterface.getHardwareAddress();
            StringBuffer sb = new StringBuffer();
            for(int i=0; i<mac.length; i++) {
                if(i!=0) {
                    sb.append("-");
                }
                //字节转换为整数
                int temp = mac[i]&0xff;
                String str = Integer.toHexString(temp);
                if(str.length()==1) {
                    sb.append("0"+str);
                }else {
                    sb.append(str);
                }
            }
            return sb.toString().toUpperCase();
        } catch (SocketException e) {
            System.out.println(e.toString());
        }
        return null;
    }

    /**
     * 判断本机系统是否是windows
     * @return
     */
    public static boolean isWindows(){
        String os = System.getProperty("os.name");
        if(os.toLowerCase().startsWith("win")){
            return true;
        }
        return false;
    }
}
